﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using DDD;
using FloatComparer = UnitTest.TestHelper.FloatComparer;

/**
 * 少なくともNVIDIAのGT640ではRGB9e5フォーマットのフレームバッファーは
 * サポートされていないのでコメントアウト
 * */

namespace UnitTest.TestPolygonMode {
    [TestClass]
    public class TestClearColor_RGB9e5_R11G11B10f_RGB10A2_RGB10A2ui_ {

        #region Field
        GameWindow window;
        World wld;
        Graphics3D g3d;
        FrameBuffer fbuf;
        #endregion

        #region Setup
        [TestInitialize ()]
        public void MyClassInitialize () {
            window = new GameWindow ();
            wld = CreateWorld ();
            fbuf = CreateFrameBuffer ();

            g3d = Graphics3D.GetInstance ();
            g3d.Reset ();

            g3d.RenderPass += delegate {
                g3d.SetFrameBuffer (fbuf);
                g3d.SetWorld (wld);
                g3d.SetViewort (0, 0, 1, 1);
            };

        }

        [TestCleanup ()]
        public void MyClassCleanup () {
            window.Dispose ();
            window = null;
            wld = null;
        }

        private static FrameBuffer CreateFrameBuffer () {
            var fbuf = new FrameBuffer (4, 0, 0);
            //var color0 = new Image2D<float> (DDD.PixelFormat.RGB9e5, 1, 1);
            var color1 = new Image2D<float> (DDD.PixelFormat.R11G11B10f, 1, 1);
            var color2 = new Image2D<float> (DDD.PixelFormat.RGB10A2, 1, 1);
            var color3 = new Image2D<ushort> (DDD.PixelFormat.RGB10A2ui, 1, 1);
            //fbuf.SetColorBuffer (0, color0);
            fbuf.SetColorBuffer (1, color1);
            fbuf.SetColorBuffer (2, color2);
            fbuf.SetColorBuffer (3, color3);
            //color0.SetClearColor (new float[3] { 1, 2, 3 });
            color1.SetClearColor (new float[3] { 1, 2, 3 });
            color2.SetClearColor (new float[4] { 0.1f, 0.2f, 0.3f, 0.4f });
            color3.SetClearColor (new ushort[4] { 1, 2, 3, 4 });
            fbuf.SetClear (true, true, true);
            return fbuf;
        }


        private World CreateWorld () {
            var cam = new Camera ();
            cam.SetPerspective (45, 1, 0.1f, 10f);

            var camNode = new Node ();
            camNode.Attach (cam);
            camNode.SetTranslation (0, 0, 1);

            var wld = new World ();
            wld.AddChild (camNode);
            wld.SetActiveCamera (camNode);

            return wld;
        }

        #endregion



        #region TestMethod
        [TestMethod]
        public void TestMethod1 () {

            g3d.Render ();
            window.SwapBuffers ();

            //var expected0 = new float[3] { 1, 2, 3 };
            var expected1 = new float[3] { 1, 2, 3 };
            var expected2 = new float[4] { 0.1f, 0.2f, 0.3f, 0.333f };  // A=3bit
            var expected3 = new ushort[4] { 1, 2, 3, 3 };               // A=3bit
            var colors0 = new float[3];
            var colors1 = new float[3];
            var colors2 = new float[4];
            var colors3 = new ushort[4];

            //var color0 = fbuf.GetColorBuffer (0) as Image2D<float>;
            var color1 = fbuf.GetColorBuffer (1) as Image2D<float>;
            var color2 = fbuf.GetColorBuffer (2) as Image2D<float>;
            var color3 = fbuf.GetColorBuffer (3) as Image2D<ushort>;
            //color0.Capture ();
            color1.Capture ();
            color2.Capture ();
            color3.Capture ();
            //color0.Get (0, 0, 1, 1, colors0);
            color1.Get (0, 0, 1, 1, colors1);
            color2.Get (0, 0, 1, 1, colors2);
            color3.Get (0, 0, 1, 1, colors3);

            //CollectionAssert.AreEqual (expected0, colors0, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected1, colors1, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected2, colors2, new FloatComparer (0.01f));
            CollectionAssert.AreEqual (expected3, colors3);
        }
        #endregion



    }
}
